package com.chw.admin.menu;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chw.admin.permission.SysPermission;
import com.chw.admin.permission.SysPermissionMapper;
import com.chw.common.ServiceData;
import com.chw.constants.Constants;
import com.chw.utils.CommonFieldsUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 服务类
 */
@Service
public class SysMenuService implements ISysMenuService {

	@Autowired
	private SysMenuMapper sysMenuMapper;
	
	@Autowired
	private SysPermissionMapper sysPermissionMapper;

	/**
	 * 根据主键获取实体对象
	 */
	public SysMenu selectById(String id) throws Exception {
		return sysMenuMapper.selectById(id);
	}

	@Override
	public List<SysMenu> find(SysMenu record) throws Exception {
		//过滤无菜单权限的用户
		String userid = CommonFieldsUtil.getCurrentUserId();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userid", userid);
		map.put("level", record.getLevel());
		map.put("parentId", record.getParentId());
		return sysMenuMapper.findByUser(map);
	}

	@Override
	public PageInfo page(SysMenu record, int currentPage, int pageSize) throws Exception {
		PageHelper.startPage(currentPage, pageSize);
		List<Map<String,Object>> list = sysMenuMapper.findMap(record);
		return new PageInfo(list);
	}

	@Override
	public ServiceData delete(String id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("enabledFlag", Constants.ENABLED_FLAG_N);
		// 删除菜单
		sysMenuMapper.updateSelective(map);
		if (hasSub(id)) {
			// 删除子菜单
			map.put("id", null);
			map.put("w_parentId", id);
			sysMenuMapper.updateSelective(map);
		}
		return null;
	}

	@Override
	public void updateStatus(String id, String status) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("status", status);
		sysMenuMapper.updateSelective(map);
		if (hasSub(id)) {
			map.put("id", null);
			map.put("w_parentId", id);
			sysMenuMapper.updateSelective(map);
		}
	}

	@Override
	public boolean hasSub(String id) throws Exception {
		SysMenu menu = new SysMenu();
		menu.setParentId(id);
		List<SysMenu> menus = sysMenuMapper.find(menu);
		if (menus != null && !menus.isEmpty()) {
			return true;
		}
		return false;
	}

	@Override
	@Transactional(value="tourTransactionManager")
	public void saveOrUpdate(SysMenu record) throws Exception {
		if (record != null) {
			if (StringUtils.isNotBlank(record.getId())) {
				CommonFieldsUtil.setUpdateValue(record);
				sysMenuMapper.updateByIdSelective(record);
			} else {
				CommonFieldsUtil.setCreateValue(record);
				sysMenuMapper.insert(record);
				//新增菜单权限模块
				SysPermission permission = new SysPermission();
				CommonFieldsUtil.setCreateValue(permission);
				permission.setRelId(record.getId());
				permission.setCategory(Constants.PERMISSION_CATEGORY_MENU);
				sysPermissionMapper.insert(permission);
			}
		}
	}

	@Override
	public boolean hasMenuName(String name,String id) throws Exception {
		SysMenu menu = new SysMenu();
		menu.setName(name);
		List<SysMenu> menus = sysMenuMapper.find(menu);
		if(menus!=null && menus.size()==1)
		{
			//当做修改操作检查名称是否存在时去除id本身
			if(StringUtils.isNotBlank(id))
			{
				menu = menus.get(0);
				if(id.equals(menu.getId())){
					return false;
				}
			}
			return true;
		}
		return false;
	}
}